<template>
  <div class="image-tools-view">
    <div class="page-header">
      <h1 class="title">图片工具</h1>
      <p class="description">
        提供一系列图片处理工具，包括压缩、裁剪、格式转换和批量下载，帮助您轻松处理图片
      </p>
    </div>
    
    <div class="tools-navigation">
      <el-tabs 
        v-model="selectedTool" 
        class="tool-tabs"
        @tab-change="handleToolChange"
        type="card"
      >
        <el-tab-pane 
          v-for="tool in tools" 
          :key="tool.value" 
          :label="tool.label" 
          :name="tool.value"
        >
          <template #label>
            <div class="tab-label">
              <el-icon><component :is="tool.icon" /></el-icon>
              <span>{{ tool.label }}</span>
              <span v-if="!tool.available" class="coming-soon-badge">开发中</span>
            </div>
          </template>
        </el-tab-pane>
      </el-tabs>
    </div>
    
    <div class="tool-container">
      <component 
        :is="currentToolComponent" 
        v-if="currentToolComponent"
        @tool-action="handleToolAction"
      />
      <el-empty 
        v-else 
        description="工具加载中" 
      />
    </div>
  </div>
</template>

<script setup>
import { ref, defineAsyncComponent, computed, onMounted, nextTick } from 'vue';
import { useStore } from 'vuex';
import { useRoute, useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { 
  PictureFilled, 
  Crop, 
  RefreshRight, 
  Download,
  Guide
} from '@element-plus/icons-vue';

// 工具组件定义
const ImageCompressor = defineAsyncComponent(() => import('../../components/tools/image/ImageCompressor.vue'));
const ImageCropper = defineAsyncComponent(() => import('../../components/tools/image/ImageCropper.vue'));
const ImageConverter = defineAsyncComponent(() => import('../../components/tools/image/ImageConverter.vue'));
const ImageDownloader = defineAsyncComponent(() => import('../../components/tools/image/ImageDownloader.vue'));

const store = useStore();
const route = useRoute();
const router = useRouter();

// 工具列表
const tools = [
  { value: 'compressor', label: '图片压缩', icon: 'PictureFilled', component: ImageCompressor, available: true },
  { value: 'cropper', label: '图片裁剪', icon: 'Crop', component: ImageCropper, available: true },
  { value: 'converter', label: '格式转换', icon: 'RefreshRight', component: ImageConverter, available: true },
  { value: 'downloader', label: '批量下载', icon: 'Download', component: ImageDownloader, available: true }
];

// 当前选中的工具
const selectedTool = ref('compressor');

// 根据工具值获取对应组件
const getToolComponent = (toolValue) => {
  const tool = tools.find(t => t.value === toolValue);
  return tool ? tool.component : null;
};

// 当前工具组件
const currentToolComponent = computed(() => {
  return getToolComponent(selectedTool.value);
});

// 监听工具切换
const handleToolChange = (toolValue) => {
  selectedTool.value = toolValue;
  
  // 更新URL参数但不导航
  const query = { ...route.query, tool: toolValue };
  router.replace({ query });
  
  // 跟踪工具使用情况
  store.dispatch('trackToolUsage', {
    category: 'image',
    tool: toolValue,
    timestamp: new Date().toISOString()
  });
  
  // 如果工具不可用，显示提示
  const tool = tools.find(t => t.value === toolValue);
  if (tool && !tool.available) {
    ElMessage.info(`${tool.label}功能正在开发中，即将上线！`);
  }
};

// 处理工具操作
const handleToolAction = (action) => {
  console.log('工具操作:', action);
  
  // 可以根据不同工具的操作进行处理
  if (action.type === 'success') {
    ElMessage.success(action.message || '操作成功');
  } else if (action.type === 'error') {
    ElMessage.error(action.message || '操作失败');
  }
};

// 生命周期钩子
onMounted(async () => {
  // 从URL参数中获取工具
  if (route.query.tool && tools.some(t => t.value === route.query.tool)) {
    selectedTool.value = route.query.tool;
  }
  
  // 页面加载完成后更新工具使用记录
  await nextTick();
  store.dispatch('incrementPageView', {
    page: 'ImageToolsView',
    timestamp: new Date().toISOString()
  });
});
</script>

<style scoped>
.image-tools-view {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.page-header {
  margin-bottom: 30px;
  text-align: center;
}

.title {
  font-size: 28px;
  color: #303133;
  margin-bottom: 10px;
}

.description {
  font-size: 16px;
  color: #606266;
  max-width: 700px;
  margin: 0 auto;
}

/* 工具导航栏样式 */
.tools-navigation {
  margin-bottom: 32px;
  border-radius: 8px;
  background-color: #f5f7fa;
  padding: 4px;
}

.tool-tabs {
  width: 100%;
}

:deep(.el-tabs__nav) {
  width: 100%;
  display: flex;
}

:deep(.el-tabs__item) {
  flex: 1;
  text-align: center;
  height: 56px;
  transition: all 0.3s;
}

:deep(.el-tabs--card > .el-tabs__header .el-tabs__item) {
  border: none;
  border-radius: 8px;
  margin: 4px;
  background-color: transparent;
}

:deep(.el-tabs--card > .el-tabs__header .el-tabs__item.is-active) {
  background-color: white;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  color: #3498db;
}

:deep(.el-tabs--card > .el-tabs__header) {
  border: none;
}

.tab-label {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  position: relative;
}

.tab-label .el-icon {
  font-size: 20px;
}

.coming-soon-badge {
  position: absolute;
  top: -6px;
  right: -10px;
  background-color: #e6a23c;
  color: white;
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 10px;
  font-weight: bold;
  transform: scale(0.85);
}

.tool-container {
  min-height: 500px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .tab-label {
    flex-direction: row;
    gap: 8px;
  }
  
  :deep(.el-tabs__item) {
    height: 40px;
  }
  
  .title {
    font-size: 24px;
  }
  
  .description {
    font-size: 14px;
  }
}

/* 新增快速使用指南样式 */
.usage-guide {
  margin: 20px 0 40px;
  padding: 20px;
  background-color: #f5f7fa;
  border-radius: 8px;
}

.guide-title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.guide-steps {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}

.step-item {
  flex: 1;
  min-width: 200px;
  display: flex;
  align-items: flex-start;
  padding: 0 15px;
  position: relative;
}

.step-item:not(:last-child)::after {
  content: '';
  position: absolute;
  right: 0;
  top: 25px;
  width: 1px;
  height: 40px;
  background-color: #e4e7ed;
}

.step-number {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background-color: #3498db;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  margin-right: 12px;
  flex-shrink: 0;
}

.step-content h4 {
  margin: 0 0 5px;
  color: #303133;
  font-size: 16px;
}

.step-content p {
  margin: 0;
  color: #606266;
  font-size: 14px;
}

@media (max-width: 768px) {
  .guide-steps {
    flex-direction: column;
  }
  
  .step-item {
    margin-bottom: 15px;
  }
  
  .step-item:not(:last-child)::after {
    display: none;
  }
}
</style> 